package sdkpay;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;




/**
 * 爱思ios充值回调逻辑
 * @author zhengjiangyu
 *
 */

public class RSASignature
{
	public static final String  SIGN_ALGORITHMS = "SHA1WithRSA";
	static String publicKey = "C05D9B52C36536807CD0444396034FB0";//itools公钥

	/**
	 * RSA签名
	 * 
	 * @param content
	 *            待签名数据
	 * @param privateKey
	 *            商户私钥
	 * @param encode
	 *            字符集编码
	 * @return 签名值
	 */
	public static String sign(String content, String privateKey, String encode) {
		String charset = "utf-8";
		if (!KYpay.isBlank(encode)) {
			charset = encode;
		}
		try {
			PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(util.Base64.decode(privateKey));

			KeyFactory keyf = KeyFactory.getInstance("RSA");
			PrivateKey priKey = keyf.generatePrivate(priPKCS8);

			java.security.Signature signature = java.security.Signature
					.getInstance(SIGN_ALGORITHMS);

			signature.initSign(priKey);
			signature.update(content.getBytes(charset));

			byte[] signed = signature.sign();

			return util.Base64.encode(signed);
		} catch (Exception e) {
		}

		return null;
	}
	public static boolean doCheck(String content, String sign, String publicKey) {
		try {
			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
			byte[] encodedKey = util.Base64.decode(publicKey);
			PublicKey pubKey = keyFactory
					.generatePublic(new X509EncodedKeySpec(encodedKey));

			java.security.Signature signature = java.security.Signature
					.getInstance(SIGN_ALGORITHMS);

			signature.initVerify(pubKey);
			signature.update(content.getBytes());

			boolean bverify = signature.verify(util.Base64.decode(sign));
			return bverify;

		} catch (Exception e) {
		}

		return false;
	}
	/**
	 * RSA验签名检查
	 * 
	 * @param content
	 *            待签名数据
	 * @param sign
	 *            签名值
	 * @param publicKey
	 *            分配给开发商公钥
	 * @param encode
	 *            字符集编码
	 * @return 布尔值
	 */
	public static boolean doCheck(String content, String sign,
			String publicKey, String encode) {
		String charset = "utf-8";
		if (!KYpay.isBlank(encode)) {
			charset = encode;
		}
		try {
			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
			byte[] encodedKey =util. Base64.decode(publicKey);
			PublicKey pubKey = keyFactory
					.generatePublic(new X509EncodedKeySpec(encodedKey));

			java.security.Signature signature = java.security.Signature
					.getInstance(SIGN_ALGORITHMS);

			signature.initVerify(pubKey);
			signature.update(content.getBytes(charset));

			boolean bverify = signature.verify(util.Base64.decode(sign));
			return bverify;

		} catch (Exception e) {
		}

		return false;
	}


//	/**
//	* 得到公钥
//	*/
//	public static PublicKey getPublicKey() throws Exception 
//	{
//		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//		byte[] encodedKey = Base64.decodeBase64(RSA_PUBLIC);
//		PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
//
//		return pubKey;
//	}

//	/**
//	* RSA 公钥解密
//	*/
//	public static String decrypt(String content) throws Exception 
//	{
//        PublicKey pubKey = getPublicKey();
//
//        Cipher cipher = Cipher.getInstance("RSA");
//        cipher.init(Cipher.DECRYPT_MODE, pubKey);
//
//        InputStream ins = new ByteArrayInputStream(Base64.decodeBase64(content));
//        ByteArrayOutputStream writer = new ByteArrayOutputStream();
//        byte[] buf = new byte[128];
//        int bufl;
//
//        while ((bufl = ins.read(buf)) != -1) {
//            byte[] block = null;
//
//            if (buf.length == bufl) {
//                block = buf;
//            } else {
//                block = new byte[bufl];
//                for (int i = 0; i < bufl; i++) {
//                    block[i] = buf[i];
//                }
//            }
//
//            writer.write(cipher.doFinal(block));
//        }
//
//        return new String(writer.toByteArray(), "utf-8");
//    }

//    /**
//    * RSA 签名�?��
//    */
//    public static boolean verify(String content, String sign) throws Exception
//    {
//    	PublicKey pubKey = getPublicKey();
//
//    	try {
//    		java.security.Signature signature = java.security.Signature
//			.getInstance(SIGN_ALGORITHMS);
//
//			signature.initVerify(pubKey);
//			signature.update(content.getBytes("utf-8"));
//
//			boolean result = signature.verify(Base64.decodeBase64(sign));
//
//			return result;
//    	} catch (Exception e) {
//    		e.printStackTrace();
//    	}
//
//    	return false;
//    }
    

}
